// package-owner/pages/owner-earnings-log/index.js
import { getUserEarningLogList, getUserEarningStatistics } from '../../../api/usr';
import { getDateMonthStartAndEnd } from '../../../biztools/common';
const { hidePullDownLoading, showLoading, hideLoading, navToAction } = require('../../../common/env');
import { ACCOUNT_LOG_CATEGORY } from '../../../constants/common';
import { PACKAGE_OWNER_PAGE } from '../../../constants/route';
const app = getApp();
Page({
    data: {
        recordList: [],
        year: '',
        month: '',
        income: 0,
        expend: 0,
    },
    onLoad: async function (options) {
        await this.refreshListData();
    },

    refreshListData: async function () {
        const { year, month } = this.data;
        const { startTime, endTime } = getDateMonthStartAndEnd(year, month);
        const param = {
            begin: startTime,
            end: endTime,
        };
        showLoading('加载中...');
        const list = await getUserEarningLogList(param);
        const res = await getUserEarningStatistics(param);
        console.log('OwnerWarningsLog => 获取的数据为：', list);
        this.setData({
            recordList: list,
            income: res.income,
            expend: res.outlay,
        });
        hideLoading();
    },

    monthChange: function (event) {
        const { year, month } = event.detail;
        console.log('OwnerWarningsLog => 年月改变', year, month);
        this.setData({
            year,
            month,
        }, async () => {
            await this.refreshListData();
        });
    },
    // 下拉刷新
    onPullDownRefresh: async function () {
        console.log('OwnerWarningsLog => 下拉刷新');
        await this.refreshListData();
        hidePullDownLoading();
    },
    gotoDetail(event) {
        const id = parseInt(event.detail);
        const { recordList } = this.data;
        const itemInfo = recordList.filter(item => item.id == id)[0];
        console.log('OwnerWarningLog => 点击的对象为:', itemInfo);
        if (itemInfo.category !== ACCOUNT_LOG_CATEGORY.redemption) {
            navToAction(PACKAGE_OWNER_PAGE.ownerEarningsDetail, {}, {
                success(res) {
                    setTimeout(() => {
                        app.event.trigger('sendOwnerEarningDetail', itemInfo);
                    }, 800);
                },
            });
        }
    },
})